package com.lsk.gateway.Filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;





//@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入全局过滤器");
        System.out.println("请求路径："+exchange.getRequest().getPath());
        System.out.println("请求方法："+exchange.getRequest().getMethod().name());
        System.out.println("请求参数："+exchange.getRequest().getQueryParams());
        long start = System.currentTimeMillis();
        return chain.filter(exchange).doFinally((e) -> {
            System.out.println("finally");
            ServerHttpResponse response = exchange.getResponse();
            System.out.println("响应码：" + response.getStatusCode());
            System.out.println("响应头：" + response.getHeaders());
            System.out.println("响应时间：" + (System.currentTimeMillis() - start)+"ms");
        });
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
